<?php
require_once(dirname(dirname(__FILE__)) . '/app.php');
require_once(dirname(__FILE__) . '/paybank.php');

$id = intval($_GET['id']);
$order = Table::Fetch('order', $id);

if (!$order) { 
	Session::Set('error', '订单不存在');
	redirect( WEB_ROOT . '/index.php' );
}
if ( $order['user_id'] != $login_user['id']) {
	
	redirect( WEB_ROOT . "/team.php?id={$order['team_id']}");
}
$team = Table::Fetch('team', $order['team_id']);
$team['state'] = team_state($team);
if ( $team['close_time'] ) {
	
	redirect( WEB_ROOT . "/team.php?id={$order['team_id']}");
}


if ( $order['state'] == 'unpay' ) {

	if($order['is_pin'] == 1)
	{
		$pp_check = DB::LimitQuery('pin_order_list',array(
						'condition' => array('order_id'=>$order['id']),
						'one'=>true,
					));
		if($pp_check) {
			$pin_id = $pp_check['pin_id'];
			
			$tmp_pin_order = Table::Fetch('pin_order', $pin_id);

			if($tmp_pin_order['state'] == 1)
			{
				//$pin_id = 0;//重新开团
				Table::Delete('order', $order['id']);
				Session::Set('error', '该拼团已成功，请重新开团！');
				redirect( WEB_ROOT . "/team.php?id={$order['team_id']}");
			}
			else if($tmp_pin_order['state'] == 2 || $tmp_pin_order['endtime'] <= time())
			{
				
					Table::Delete('order', $order['id']);	
				
				Session::Set('error', '该拼团已失败，请重新开团！');
				redirect( WEB_ROOT . "/team.php?id={$order['team_id']}");
				//$pin_id = 0;//重新开团
			}
		}	
	}

	/* payservice choice */
	if(@$INI[$order['service']]['mid'] || @$INI[$order['service']]['acc']) {
		$ordercheck[$order['service']] = 'checked';
	}
	else {
		foreach($payservice AS $pone) {
			if(@$INI[$order['service']]['mid'] || @$INI[$order['service']]['acc']) { $ordercheck[$order['service']] = 'checked'; }
		}
	}
	$credityes = ($login_user['money'] >= $order['origin']);
	$creditonly = ($team['team_type'] == 'seconds' && option_yes('creditseconds'));

	/* generator unique pay_id */
	if (! ($order['pay_id'] 
				&& (preg_match('#-(\d+)-(\d+)-#', $order['pay_id'], $m) 
					&& ( $m[1] == $order['id'] && $m[2] == $order['quantity']) )
		  )) {
		$randid = strtolower(Utility::GenSecret(4, Utility::CHAR_WORD));
		$pay_id = "go-{$order['id']}-{$order['quantity']}-{$randid}";
		Table::UpdateCache('order', $order['id'], array(
					'pay_id' => $pay_id,
					));
	}
	//express_id
	if(isset($_GET['aid'])) {
		$address_info = Table::Fetch('address',$_GET['aid']);	
		
		Table::UpdateCache('order', $order['id'], array(
					'aid' => intval($_GET['aid']),
					'realname' => $address_info['name'],
					'mobile' => $address_info['mobile'],
					'address' => $address_info['province'].$address_info['city'].$address_info['area'].$address_info['street'],
					));
		$order['realname'] = $address_info['name'];
		$order['mobile'] = $address_info['mobile'];
		$order['address'] = $address_info['province'].$address_info['city'].$address_info['area'].$address_info['street'];
		
		//$order = Table::Fetch('order', $id);
	}
	$express = Table::Fetch('address',$order['express_id']);
	
	/* update area limit by fish 201508 begin*/
	$is_js_arealimit = 0;
	$cityname = $city['name'];
	if(isset($INI['system']['areacondlimit']) && $INI['system']['areacondlimit']==1 && $cityname !='全国')
	{
		$is_js_arealimit = 1;
	}
	/* update area limit by fish 201508 end*/
	
	//order/check.php?id=ZTMTQ0 
	$refurl = '/order/check.php?id='.$order['id'];
	
	//到这一步先清空订单中的红包券
	Table::UpdateCache('order', $order['id'], array(
					'card_id' => '',
					'card' =>0,
		));
	
	$condition = array( 'uid' => $login_user_id);
	$condition[] = ' consume ="N" and begin_time<'.time().' and  end_time >'.time().' and credit < '.$order['origin'];
	
	$first_card = array();
	
	$cards_list = DB::LimitQuery('card', array(
		'condition' => $condition,
		'order' => 'ORDER BY addtime DESC',
		'size' => 100,
	));
	if(!empty($cards_list))
	{
		$first_card = current($cards_list);
	}
	die(include template('order_check'));
}
redirect( WEB_ROOT . "/order/view.php?id={$id}");
